using System.Text;

namespace OracleInternal.SqlAndPlsqlParser.LocalParsing
{
	public class OracleLpName
	{
		protected string m_vDbName;

		protected string m_vRawName;

		protected OracleLpCaseSensitivity m_vCS;

		public string DbName
		{
			get
			{
				return m_vDbName;
			}
			internal set
			{
				m_vDbName = value;
				m_vRawName = m_vDbName;
				if (m_vDbName != m_vDbName.ToUpperInvariant())
				{
					m_vCS = OracleLpCaseSensitivity.Sensitive;
				}
			}
		}

		public string RawName
		{
			get
			{
				return m_vRawName;
			}
			internal set
			{
				m_vRawName = value;
				m_vDbName = GetDbName(m_vRawName, out m_vCS);
			}
		}

		public object CaseSensitivity => m_vCS;

		public OracleLpName()
		{
		}

		public OracleLpName(string rawName)
		{
			RawName = rawName;
		}

		public static string GetDbName(string name, out OracleLpCaseSensitivity cs)
		{
			if (string.IsNullOrEmpty(name))
			{
				cs = OracleLpCaseSensitivity.Unknown;
				return name;
			}
			int length = name.Length;
			if (length >= 2 && name[0] == '"' && name[length - 1] == '"')
			{
				cs = OracleLpCaseSensitivity.Sensitive;
				return name.Substring(1, length - 2);
			}
			return GetDbText(name, out cs);
		}

		public static string GetDbText(string text, out OracleLpCaseSensitivity cs)
		{
			int length = text.Length;
			bool flag = false;
			bool flag2 = false;
			bool flag3 = false;
			StringBuilder stringBuilder = new StringBuilder(length);
			foreach (char c in text)
			{
				switch (c)
				{
				case '\'':
					if (flag2)
					{
						flag2 = false;
					}
					else if (!flag)
					{
						flag2 = true;
					}
					break;
				case '"':
					if (flag)
					{
						flag = false;
					}
					else if (!flag2)
					{
						flag = true;
					}
					break;
				}
				if (flag)
				{
					flag3 = true;
					stringBuilder.Append(c);
				}
				else if (!char.IsWhiteSpace(c))
				{
					stringBuilder.Append(char.ToUpperInvariant(c));
				}
			}
			cs = (flag3 ? OracleLpCaseSensitivity.Sensitive : OracleLpCaseSensitivity.Insensitive);
			return stringBuilder.ToString();
		}

		public override string ToString()
		{
			return m_vDbName;
		}
	}
}
